#include <bits/stdc++.h>
using namespace std;

char buf[(int) 5e7], *fs = buf;

inline int gn() {
    int k = 0;
    register char c = *fs++;
    for (; !isdigit(c); c = *fs++);
    for (; isdigit(c); c = *fs++) k = k * 10 + c - '0';
    return k;
}

int a[4001], b[4001], c[4001], d[4001], N, S, cntc, cntd, ans, ta, tb;
bool visa[4001], visb[4001];

void judge() {
    int tmp = 0;
    for (int i = 1; i <= N; i++)
        if (a[i] > b[i])
            tmp++;
    if (tmp == S) ans++;
}

void dfs2(int n) {
    if (n == cntd + 1) {
        judge();
        return;
    }
    for (int i = 1; i <= N; i++) {
        if (visb[i]) continue;
        b[d[n]] = i;
        visb[i] = 1;
        dfs2(n + 1);
        visb[i] = 0;
    }
}

void dfs1(int n) {
    if (n == cntc + 1) {
        dfs2(1);
        return;
    }
    for (int i = 1; i <= N; i++) {
        if (visa[i]) continue;
        a[c[n]] = i;
        visa[i] = 1;
        dfs1(n + 1);
        visa[i] = 0;
    }
}

int main() {
    freopen("b.in", "r", stdin);
    freopen("b.out", "w", stdout);
    fread(buf, 1, (int) 5e7, stdin);   
    N = gn(), S = gn();
    for (int i = 1; i <= N; i++) {
        a[i] = gn();
        if (!a[i]) c[++cntc] = i;
        visa[a[i]] = 1;
    }
    for (int i = 1; i <= N; i++) {
        b[i] = gn();
        if (!b[i]) d[++cntd] = i;
        visb[b[i]] = 1;
    }
    dfs1(1);
    cout << ans << '\n';
}
